<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Action\PageJumpAction;
use App\Admin\Extensions\Form\Course\Edit;
use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\Course;
use App\Models\CourseSection;
use App\Models\Mutual;
use App\Models\Video;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class VideoController extends BaseController
{


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('图文列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('图文详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑图文')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增图文')
            ->description('新增图文')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Video());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', '标题');
                $filter->equal('campusInfo.id', '校区')->select(Campus::where('status', 1)->pluck('name as text', 'id'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select(['禁用', '正常']);
            });

        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', 'ID')->sortable();
        $grid->column('title', '标题');
        $grid->column('campusInfo.name', '校区')->sortable();
        $grid->column('pics', '图片集')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('video_url', '视频')->display(function ($v, $column) {
            if (!empty($v)) {
                return $column->copyable();
            }
            return '-';

        });
        $grid->column('read_num', '浏览量')->sortable();
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
        $grid->column('created_at', __('created_at'))->sortable();
        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        $grid->disableRowSelector();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Course::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Video());
        $form->text('title', '标题')->required()->attribute(['autocomplete' => 'off']);
        $form->select('campus_id', '校区')->options(Campus::where('status', 1)->pluck('name as text', 'id'))->required();
        $form->multipleImage('pics', '图片集')->uniqueName()->removable();
        $form->file('video_url', '视频')->uniqueName()->removable();
        $form->radio('status', '状态')->options(GlobalConstant::_STATUS)->default(1);
        $form->text('desc', '图文描述');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        $form->saving(function (Form $form) {
            if ($form->isCreating() && empty($form->pics) && empty($form->video_url)) {
                admin_error('请上传图片或者视频');
                return back();
            }
        });
        return $form;
    }
}
